""" DateNotifier Notification """

from S3D.DateNotifier import _Str2Int, _LoadQIcon
from PyQt4.QtGui import *
from S3D.DateNotifier.UI.Notification import Ui_Notification
from S3D.DateNotifier.IconSelectionDialog import IconSelectionDialog

class Notification(QWidget):
    class Params:
        __slots__ = ('enabled', 'startIn', 'alwaysIn', 'notifyEach', 'groupIcon')

        def __init__(self):
            self.enabled        = False
            self.startIn        = 3
            self.alwaysIn       = 1
            self.notifyEach     = 1
            self.groupIcon      = ''

    def __init__(self, parent, tag, params = None):
        QWidget.__init__(self)
        self.ui = Ui_Notification()
        self.ui.setupUi(self)

        self.params = params if params is not None else self.Params()

        self.isInitializing = True

        self.parent = parent
        self.tag = tag
        self.ui.nameBox.setTitle(tag)

        self.ui.nameBox.setChecked(self.params.enabled)
        self.ui.startIn.setText(str(self.params.startIn))
        self.ui.notifyEach.setText(str(self.params.notifyEach))

        self.isInitializing = False

        self.updateIcons()

    def __lt__(self, p):
        if self.params.enabled != p.params.enabled:
            return self.params.enabled > p.params.enabled
        if self.params.startIn != p.params.startIn:
            return self.params.startIn > p.params.startIn
        return self.tag < p.tag

    def updateIcons(self):
        if self.params.groupIcon == '':
            self.ui.groupIcon.setIcon(QIcon())
        else:
            self.ui.groupIcon.setIcon(_LoadQIcon(self.params.groupIcon))

    def dataUpdated(self):
        if self.isInitializing:
            return
        
        self.params.enabled     = self.ui.nameBox.isChecked()
        self.params.startIn     = max(_Str2Int(self.ui.startIn.text()), 1)
        self.params.alwaysIn    = min(_Str2Int(self.ui.alwaysIn.text()), self.params.startIn) 
        self.params.notifyEach  = max(_Str2Int(self.ui.notifyEach.text()), 1)

        if self.parent and not self.parent.isLoading:
            self.parent.autosave()

    def browseGroupIcon(self):
        dlg = IconSelectionDialog(self.params.groupIcon, self)
        if not dlg.exec_():
            return

        self.params.groupIcon = dlg.selectedIcon
        self.updateIcons()

        if self.parent and not self.parent.isLoading:
            self.parent.autosave()

if __name__ == '__main__':
    import sys
    qApp = QApplication(sys.argv)
    dlg = QDialog()

    dlg.setLayout(QVBoxLayout())
    dlg.layout().addWidget(Notification(None, 'Test'))
    dlg.exec_()
